/* eslint-disable */
const id = 'set-customer-watermark-id'
const setWatermark = (dom, str, option = {
  width: 200,
  height: 150
}) => {
  if (document.getElementById(id) !== null) {
    dom ? dom.removeChild(document.getElementById(id)) : document.body.removeChild(document.getElementById(id))
  }
  // 创建一个画布
  const can = document.createElement('canvas')
  can.width = option.width
  can.height = option.height

  // 设置画布样式
  const cans = can.getContext('2d')
  cans.rotate((-30 * Math.PI) / 180)
  cans.font = '12px Microsoft YaHei'
  cans.fillStyle = 'rgba(0,0,0,0.1)'
  cans.textAlign = 'left'
  cans.textBaseline = 'middle'
  cans.fillText(str, 0, can.height)

  // 创建整体容器，设置样式
  const div = document.createElement('div')
  div.id = id
  div.style.pointerEvents = 'none'
  div.style.top = '0px'
  div.style.left = '0px'
  div.style.position = 'absolute'
  div.style.zIndex = '1000'
  div.style.width = document.documentElement.clientWidth + 'px'
  div.style.height = document.documentElement.clientHeight + 'px'
  div.style.background = 'url(' + can.toDataURL('image/png') + ') left top repeat'

  dom ? dom.appendChild(div) : document.body.appendChild(div)
  return id
}

const watermark = {
  set: (dom, str) => {
    let domid = setWatermark(dom, str)
    setInterval(() => {
      if (document.getElementById(domid) === null) {
        domid = setWatermark(dom, str)
      }
    }, 500)
    window.onresize = () => {
      setWatermark(dom, str)
    }
  },
  remove: () => {
    if (document.getElementById(id) !== null) {
      ;(document.getElementById(id)).style.display = 'none'
    }
  }
}

export default watermark
